﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Set;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.View;

namespace OpenSoftStore.Client.UIL.ManageSet
{
    public partial class SystemDataEdit : UserControl
    {
        public SetLogic SetBll;
        private static SystemDataEdit instance;
        public static SystemDataEdit Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SystemDataEdit();
                }
                return instance;
            }
        }

        public SystemDataEdit()
        {
            InitializeComponent();
            SetBll = new SetLogic();
        }

        private void btnInputData_Click(object sender, EventArgs e)
        {
            SetBll.Spa.RunSqLcmd(" update T_StoreIOItem set Pos='Store1' where Pos='' or Pos is null ");
            var storeIOs = SetBll.SelectWhere<T_StoreIO>(" SELECT * from T_StoreIO where Type='出库' ");
            if (storeIOs.Any())
            {
                MessageBox.Show("已经有出库数据！");
                return;
            }
            var products = SetBll.SelectWhere<V_Product>(" SELECT ProductId,Pos,sum(Amount) as AllStore from " +
             " T_StoreIOItem inner join T_StoreIO on T_StoreIOItem.StoreIOId= T_StoreIO.Id  where T_StoreIO.Type='入库' group by ProductId,Pos ");
            //var products = SetBll.SelectWhere<V_Product>(" SELECT ProductId,Pos,sum(Amount) as AllStore from T_StoreIOItem group by ProductId,Pos ");
            foreach (var product in products)
            {
                string sql = "";
                switch (product.Pos)
                {
                    case "Store1":
                        sql = " update T_ProductExt set Store1 =" + product.AllStore + " where ProductId='" + product.ProductId + "'";
                        break;
                    case "Store2":
                        sql = " update T_ProductExt set Store2 =" + product.AllStore + " where ProductId='" + product.ProductId + "'";
                        break;
                    case "Store3":
                        sql = " update T_ProductExt set Store3 =" + product.AllStore + " where ProductId='" + product.ProductId + "'";
                        break;
                    case "Store4":
                        sql = " update T_ProductExt set Store4 =" + product.AllStore + " where ProductId='" + product.ProductId + "'";
                        break;
                    default:
                        continue;
                }
                SetBll.Spa.RunSqLcmd(sql);
            }
        }
    }
}
